package cn.lg.soar.system.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.auth.entity.TemporaryPassword;
import cn.lg.soar.system.auth.model.TemporaryPasswordVo;
import cn.lg.soar.system.auth.service.impl.TemporaryPasswordServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * @date 2021/10/16
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Api(tags = {"临时密码管理"})
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/temporary-password")
public class TemporaryPasswordController {

    @Autowired
    private TemporaryPasswordServiceImpl service;


    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated TemporaryPasswordVo data) {
        return HttpResult.<Serializable>success("创建成功")
                .setData(this.service.create(UserContext.getUser(), data));
    }

    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated TemporaryPasswordVo data) {
        this.service.update(UserContext.getUser(), data);
        return HttpResult.success("更新成功");
    }

    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        AssertUtil.isTrue(service.removeByIds(ids), "删除失败");
        return HttpResult.success("删除成功");
    }

    @DetailMapping
    public HttpResult<TemporaryPassword> detail(@Id Long id) {
        return HttpResult.data(this.service.getById(id));
    }

    @ListMapping
    public HttpResult<List<TemporaryPassword>> list(){
        return HttpResult.data(service.listByUserId(UserContext.getUserId()));
    }

}
